package com.hyg.common.utils.language;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;
import android.util.DisplayMetrics;

import androidx.annotation.Nullable;
import androidx.annotation.StringRes;

import com.hyg.common.CommonLibary;

import java.util.Locale;

public class LanguageUtil {
    public static final String TAG = "LanguageUtil";
    public static Context appContext;
    private static final String DEFAULT_COUNTRY = "US";
    private static final String DEFAULT_LANGUAGE = "en";

    /**
     * 更改应用语言
     * @param locale 语言地区
     * @param persistence 是否持久化
     */

    public static void changeAppLanguage(Locale locale, boolean persistence) {
        //获取应用程序的所有资源对象
        Resources resources = CommonLibary.getContext().getResources();
        //获取屏幕参数
        DisplayMetrics metrics = resources.getDisplayMetrics();
        //获取设置对象
        Configuration configuration = resources.getConfiguration();
        //如果API < 17
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.locale = locale;
        } else //如果 17 < = API < 25 Android 7.7.1
            if(Build.VERSION.SDK_INT<Build.VERSION_CODES.N_MR1){
                configuration.setLocale(locale);
            }else{//API 25  Android 7.7.1
                configuration.setLocale(locale);
                configuration.setLocales(new LocaleList(locale));
                appContext= CommonLibary.getContext().createConfigurationContext(configuration);
            }
        //更新配置信息
        resources.updateConfiguration(configuration, metrics);
        //持久化
        if (persistence) {
            SharePreferencesTools.saveObjectToSharePreferences(CommonLibary.getContext(), LanguageSwitchConstant.LANGUA_SWITCH_FILE_NAME, LanguageSwitchConstant.LANGUA_SWITCH_SELECTED_KEY,locale);
        }
    }

    /**
     * 获取App当前语言
     * 如果存过信息那么以存的信息为准否则取系统的配置信息为准
     * @Return Locale
     * **/
//    public static Locale getAppLocale() {
//        //读取本地配置文件信息
//        Locale currentAppLocale=null;
//        currentAppLocale=(Locale) SharePreferencesTools.readObjectFromSharePreferences(CommonLibary.getContext(), LanguageSwitchConstant.LANGUA_SWITCH_FILE_NAME, LanguageSwitchConstant.LANGUA_SWITCH_SELECTED_KEY);
//        if(currentAppLocale==null){
//            currentAppLocale=getSystemLocale();
//        }
//        return currentAppLocale;
//        return LocalManageUtil.getSetLanguageLocale(context);
//    }

    /******
     * 获取当前系统语言
     */
    public static Locale getSystemLocale(){
        Locale currentSystemLocale;
        //API >= 24
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            currentSystemLocale = Resources.getSystem().getConfiguration().getLocales().get(0);
        } else {
            currentSystemLocale = Resources.getSystem().getConfiguration().locale;
        }
        return currentSystemLocale;
    }

    /**
     * 获取英文的String
     * @param context
     * @param stringId
     * @return
     */
    public static String getStringToEnglish(Context context, int stringId) {
        Locale locale = new Locale(DEFAULT_LANGUAGE, DEFAULT_COUNTRY);
        Configuration configuration = new Configuration(context.getResources().getConfiguration());
        configuration.setLocale(locale);
        return context.createConfigurationContext(configuration).getResources().getString(stringId);
    }

    /**
     * 获取app设置的语言string
     */
    public static String getLocalString(Context context, @StringRes int stringId, @Nullable Object... formatArgs) {
        Locale locale = LocalManageUtil.getSetLanguageLocale(context);
//        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
//            locale = Locale.forLanguageTag("appDefault");
//        }else{
//            return "";
//        }
        Configuration configuration = new Configuration(context.getResources().getConfiguration());
        configuration.setLocale(locale);
        return context.createConfigurationContext(configuration).getResources().getString(stringId, formatArgs);
    }
}